java median of list

55

java median of list -

public static double Median(ArrayList<Double> values) {
    Collections.sort(values);

    if (values.size() % 2 == 1)
        return values.get((values.size() + 1) / 2 - 1);
    else {
        double lower = values.get(values.size() / 2 - 1);
        double upper = values.get(values.size() / 2);

        return (lower + upper) / 2.0;
    }
}

Comments

Submit
0 Comments